package com.yc.collections;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 针对集合框架的一些demo练习和jdk8 Stream对集合的操作
 *
 * @author yc112
 */
public class ListTest1 {


    public static void main(String[] args) {
        testGroupBy();
    }

    private static void testGroupBy() {
        Person p1 = new Person("张三", "男", 18, "135033124");
        Person p2 = new Person("李四", "女", 20, "135033125");
        Person p3 = new Person("王五", "女", 21, "135033126");
        Person p4 = new Person("张三三", "男", 18, "135033127");

        // 题外补充 ArrayList 底层是动态数组，能快速查找； LinkedList 底层是双链表结构
        List<Person> list = new ArrayList<>();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);

        Animal p5 = new Animal("张三1", "男", 18, "135033124");
        Animal p6 = new Animal("李四2", "女", 20, "135033125");
        Animal p7 = new Animal("王五3", "女", 21, "135033126");
        Animal p8 = new Animal("张三4", "男", 18, "135033127");
        List<Animal> animals = new ArrayList<>();
        animals.add(p5);
        animals.add(p6);
        animals.add(p7);
        animals.add(p8);

        //




        // 根据指定属性分组，返回一个Map集合
        Map<Integer, List<Person>> collect = list.stream().collect(Collectors.groupingBy(Person::getAge));
        collect.forEach((k, v) -> {
            System.out.println(k);
            v.stream().forEach(e -> System.out.println(e));
        });
    }


}

@Data
class Animal {
    private String name;
    private String sex;
    private Integer age;
    private String num;

    public Animal(String name, String sex, Integer age, String num) {

    }
}


@Data
class Person {
    private String name;
    private String sex;
    private Integer age;
    private String num;

    public Person() {
    }

    public Person(String name, String sex, Integer age, String num) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.num = num;
    }
}